﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class BinarySearchAlgorithm
{
    static void Main()
    {
        try
        {
            int[] arr = new int[] {10, 9, 1, 4, 4, 6, 7, 0};
            int wanted = 1;
            int index = 0;
            int middle = 0;
            int curr = 0;
            int start = 0;
            int end = arr.Length - 1;
            Array.Sort(arr);

            while (start <= end)
            {
                middle = (start + end + 1) / 2;
                curr = arr[middle];

                if (curr == wanted)
                {
                    index = middle;
                    break;
                }
                else if (curr < wanted)
                {
                    start = middle + 1;
                }
                else
                {
                    end = middle - 1;
                }
            }

            if (curr != wanted)
            {
                Console.WriteLine("Not find the search number!");
            }
            else
            {
                Console.WriteLine("The index is {0} of given element is {1}.",
                index, curr);
            }

        }
        catch (Exception)
        { 
            Console.WriteLine("Wrong data!");
            return;
        }
    }
}
